package com.iexbuy.ihg.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class WaterWave extends View {
	
	private static int MAX_ALPHA=255;
	private int alpha;
	private int radius, xDown, yDown, width;
	private Paint paint;

	public WaterWave(Context context) {
		super(context);
	}
	
	public WaterWave(Context context, AttributeSet attrs) {
        super(context, attrs);
        alpha = 0;
        radius = 0;
        initPaint();
    }

	/**
     * onMeasure方法，确定控件大小，这里使用默认的
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    /**
     * 画出需要的图形的方法，这个方法比较关键
     */
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(xDown, yDown, radius, paint);
    }
    
    /**
     * 初始化paint
     */
    private void initPaint() {
        /*
         * 新建一个画笔
         */
        paint = new Paint();
     
        paint.setAntiAlias(true);
        paint.setStrokeWidth(3);
     
        // 设置是环形方式绘制
        paint.setStyle(Paint.Style.STROKE);
     
        System.out.println(alpha= + alpha);
        paint.setAlpha(alpha);
        System.out.println("得到的透明度：" + paint.getAlpha());
     
        paint.setColor(Color.WHITE);
    }
    
    @Override
    /**
     * 触摸事件的方法
     */
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
 
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            triggerWaterWave((int) event.getX(), (int) event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
 
            break;
        case MotionEvent.ACTION_UP:
 
            break;
 
        default:
            break;
        }
 
        return true;
    }
    
    public void triggerWaterWave(int x, int y) {
    	radius = 0;
        alpha = MAX_ALPHA;
        width = radius / 4;
        xDown = x;
        yDown = y;

        handler.sendEmptyMessage(0);
    }
    
    private Handler handler = new Handler() {
    	 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 0:
                flushState();
     
                invalidate();
     
                if (alpha != 0) {
                    // 如果透明度没有到0，则继续刷新，否則停止刷新
                    handler.sendEmptyMessageDelayed(0, 25);
                }
     
                break;
     
            default:
                break;
            }
        }
     
        /**
         * 刷新状态
         */
        private void flushState() {
            radius += 3;
            alpha -= 10;
            if (alpha < 0) {
                alpha = 0;
            }
            // System.out.println(alpha= + alpha);
            width = radius / 4;
     
            paint.setAlpha(alpha);
            paint.setStrokeWidth(3);
        }
     
    };
    
    /**
     * 刷新状态
     */
    private void flushState() {
        radius += 5;
        alpha -= 10;
        if (alpha < 0) {
            alpha = 0;
        }
        // System.out.println(alpha= + alpha);
        width = radius / 4;

        paint.setAlpha(alpha);
        paint.setStrokeWidth(3);
    }
}
